package com.yxdj.ai.tool;


import com.yxdj.common.core.constant.SecurityConstants;
import com.yxdj.common.core.domain.R;
import com.yxdj.order.api.RemoteOrderService;
import com.yxdj.order.api.domain.OrderInfo;
import com.yxdj.user.api.RemoteManageUserService;
import dev.langchain4j.agent.tool.P;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AppointmentTool {

    @Autowired
    private RemoteOrderService remoteOrderService;

    @Autowired
    private RemoteManageUserService remoteManageUserService;


    @Tool(value = "预约维修服务")
    public String appointmentService(@P(value = "联系人姓名") String contactName,
                              @P(value = "联系人手机号") String contactPhone,
                              @P(value = "预约时间") String appointmentTime,
                              @P(value = "服务地址") String address,
                              @P(value = "服务类型") Long serviceType) {
        //获取用户信息
        R<User> userByPhone = remoteManageUserService.getUserByPhone(contactPhone, SecurityConstants.INNER);
        if(R.FAIL == userByPhone.getCode()){
            throw new RuntimeException(userByPhone.getMsg());
        }
        //获取用户信息
        User user = userByPhone.getData();
        //获取用户id
        Long userId = user.getId();
        //创建OrderInfo对象
        OrderInfo orderInfo = new OrderInfo();
        //设置用户id
        orderInfo.setUserId(userId);
        //设置预约时间
        orderInfo.setAppointmentTime(appointmentTime);
        //设置联系人姓名
        orderInfo.setContactName(contactName);
        //设置联系人电话
        orderInfo.setContactPhone(contactPhone);
        //设置服务地址
        orderInfo.setServiceAddress(address);
        //设置服务详情id
        orderInfo.setServiceDetailId(serviceType);
        //设置服务项名称
        orderInfo.setServiceOptionName("AI下单");
        //设置订单号
        orderInfo.setOrderNumber(String.valueOf(System.currentTimeMillis()+userId));
        //设置上面费用
        orderInfo.setServiceFee(0.0);
        //设置订单状态
        orderInfo.setOrderStatus(0);
        //保存订单
        R<Void> voidR = remoteOrderService.saveAiOrderInfo(orderInfo, SecurityConstants.INNER);
        if(R.FAIL == voidR.getCode()){
            throw new RuntimeException(voidR.getMsg());
        }
        return "已成功预约维修服务";
    }
}
